package com.qianbao.qianbaobusiness.ui.deposit;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baseandroid.config.Api;
import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.MerchantInfo;
import com.qianbao.qianbaobusiness.json.UnPayShopListBean;
import com.qianbao.qianbaobusiness.ui.webview.SimpleWebViewActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class DepositToH5Activity extends BaseMvpActivity<DepositToH5View, DepositToH5Present> implements DepositToH5View {

    @BindView(R.id.toolbar_title)
    TextView tvTitle;  //标题

    @BindView(R.id.right_text)
    TextView tvRight;  //右上角文本

    @BindView(R.id.tv_to_pay)
    TextView tvToPay;    //缴纳押金，跳转到我的押金

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbar_back;

    private UnPayShopListBean bean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_to_pay_web;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        toolbar_back.setVisibility(View.GONE);
        EventBus.getDefault().register(this);
        tvTitle.setText(DepositToH5Activity.this.getResources().getString(R.string.pay_deposit_title));
        tvRight.setText(DepositToH5Activity.this.getResources().getString(R.string.skip));
        tvRight.setTextColor(ContextCompat.getColor(this, R.color.text_support));
        tvRight.setVisibility(View.VISIBLE);
        getPresenter().getMerchantUnpayDepositInfo(getMerchantNos());
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.tv_to_pay, R.id.right_text})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_to_pay:   //去缴纳
                if ((bean.getMerchantWaitPayInfo() == null)) {
                    Intent intent = new Intent(this, IntroducedServiceActivity.class);
                    startActivity(intent);
                } else {
                    if (Global.getShopListInfo().merchantList.size() > 1) {  //当前商家名下店铺数量大于1
                        Intent intent = new Intent(this, MyDepositActivity.class);
                        intent.putExtra("bean", bean);
                        startActivity(intent);
                    } else {  //当前商家名下店铺小于1，跳转到H5 deposit
                        Intent intent = new Intent(this, SimpleWebViewActivity.class);
                        intent.putExtra(SimpleWebViewActivity.WEBVIEW_TITLE, "缴纳押金");
                        intent.putExtra(SimpleWebViewActivity.WEBVIEW_URL, Api.depositH5Url + "?merchantNo=" + Global.getShopListInfo().merchantList.get(0).merchantNo
                                                                                   + "&mobile=" + Global.getShopListInfo().merchantList.get(0).managerMobile);
                        intent.putExtra(SimpleWebViewActivity.WEBVIEW_INIT_QBPAY, true);
                        startActivity(intent);
                    }
                }
                finish();
                break;

            case R.id.right_text:
                finish();
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Integer event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void getMerchantSuccess(UnPayShopListBean result) {
        bean = result;
    }

    private String getMerchantNos() {
        String merchantNos = "";
        for (MerchantInfo merchantInfo : Global.getShopListInfo().merchantList) {
            if (TextUtils.isEmpty(merchantNos)) {
                merchantNos = merchantInfo.merchantNo;
            } else {
                merchantNos = merchantNos + "," + merchantInfo.merchantNo;
            }
        }
        return merchantNos;
    }
}
